package com.nhjf.mobile.controller.tochatwith;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nhjf.common.page.Pagination;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.tochatwith.Tochatwith;
import com.nhjf.pojo.dto.tochatwith.TochatwithDto;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.tochatwith.TochatwithQo;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.tochatwith.TochatwithService;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/msg")
public class TochatwithController {

	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private TochatwithService tochatwithService;

	/**
	 * 接收消息接口：
	 * 接收消息查看
	 * 接收消息列表
	 * 发送回复消息（添加）
	 * 接收消息删除
	 * @param token
	 * @param page
	 * @return
	 */
	

	//接收消息接口：消息列表
	@ResponseBody
	@RequestMapping("/receive_list")
	public String receivelist(@RequestParam(value="token", required=true)String  token,
			@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					System.out.println(user.getUserName());
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setReceiverId(user.getId());
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(tochatwithQo);
					pagination = tochatwithService.queryPagination(pagination);
					resMap.put("code", "200");
					resMap.put("message", pagination);
					return JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}

	}


	//接收消息接口：id查看消息
	@ResponseBody
	@RequestMapping("/view_message")
	public String viewmessage(@RequestParam(value="token", required=true)String  token
			,String chakanid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		TochatwithDto tochatwithDto=new TochatwithDto();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setReceiverId(user.getId());
					tochatwithQo.setTochatwithid(Long.parseLong(chakanid));
					Tochatwith tochatwith=tochatwithService.queryUnique(tochatwithQo);
					tochatwithDto.setReceiver(tochatwith.getReceiver());
					tochatwithDto.setSender(tochatwith.getSender());
					tochatwithDto.setMessage(tochatwith.getMessage());
					tochatwithDto.setWhethertoread(tochatwith.getWhethertoread());
					tochatwithDto.setSendingtime(tochatwith.getSendingtime());
					tochatwithDto.setMessagetype(tochatwith.getMessagetype());
					resMap.put("code", "200");
					resMap.put("message",tochatwithDto);
					return JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}


	}


	//接发消息接口：发送回复
	@ResponseBody
	@RequestMapping("/receive_reply")
	public String receivereply(@RequestParam(value="token", required=true)String  token
			,String receiveid,String message) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					Tochatwith tochatwith=new Tochatwith();
					tochatwith.setSender(user.getId());
					tochatwith.setReceiver(Long.parseLong(receiveid));
					tochatwith.setSendingtime(new Date());
					tochatwith.setMessagetype(1);
					tochatwith.setWhethertoread(1);
					tochatwith.setMessage(message);
					tochatwithService.save(tochatwith);
					resMap.put("code", "200");
					resMap.put("message","发送成功！");
					return JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message","异常抛出！");

		}
		resMap.put("code", "500");
		resMap.put("message","发送失败！");
		return JSONObject.fromObject(resMap).toString();

	}

	//接收消息删除
	@ResponseBody
	@RequestMapping("/receive_delete")
	public String receivedelete(@RequestParam(value="token", required=true)String  token
			,String deleteid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					System.out.println(user.getUserName());
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setReceiverId(user.getId());
					tochatwithQo.setId(Long.parseLong(deleteid));
					tochatwithService.deleteById(Long.parseLong(deleteid));
					resMap.put("code", "200");
					resMap.put("message","删除成功！");
					return JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		resMap.put("code", "500");
		resMap.put("message","删除失败！");
		return JSONObject.fromObject(resMap).toString();

	}

	/**
	 * 发消息接口：
	 * 发送消息列表
	 * 发送消息查看
	 * 发送消息删除
	 * @param token
	 * @param page
	 * @return
	 */


	//发送消息接口：消息列表
	@ResponseBody
	@RequestMapping("/send_list")
	public String sendlist(@RequestParam(value="token", required=true)String  token,
			@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					System.out.println(user.getUserName());
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setSenderId(user.getId());
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(tochatwithQo);
					pagination = tochatwithService.queryPagination(pagination);
					resMap.put("code", "200");
					resMap.put("message", pagination);
					return JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}

	}


	//发送消息接口：id查看消息
	@ResponseBody
	@RequestMapping("/send_view")
	public String sendview(@RequestParam(value="token", required=true)String  token
			,String sendid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		TochatwithDto tochatwithDto=new TochatwithDto();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setSenderId(user.getId());
					tochatwithQo.setTochatwithid(Long.parseLong(sendid));
					Tochatwith tochatwith=tochatwithService.queryUnique(tochatwithQo);
					tochatwithDto.setReceiver(tochatwith.getReceiver());
					tochatwithDto.setSender(tochatwith.getSender());
					tochatwithDto.setMessage(tochatwith.getMessage());
					tochatwithDto.setMessagetype(tochatwith.getMessagetype());
					tochatwithDto.setSendingtime(tochatwith.getSendingtime());
					tochatwithDto.setWhethertoread(tochatwith.getWhethertoread());
					resMap.put("code", "200");
					resMap.put("message",tochatwithDto);
					return JSONObject.fromObject(resMap).toString();

				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}
	}




	//发送消息接口：消息删除
	@ResponseBody
	@RequestMapping("/send_delete")
	public String senddelete(@RequestParam(value="token", required=true)String  token
			,String deleteid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "404");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				adminSession.getLoginName();
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					TochatwithQo tochatwithQo=new TochatwithQo();
					tochatwithQo.setSenderId(user.getId());
					tochatwithQo.setTochatwithid(Long.parseLong(deleteid));
					tochatwithService.deleteById(Long.parseLong(deleteid));
					resMap.put("code", "200");
					resMap.put("message","删除成功！");
					return JSONObject.fromObject(resMap).toString();
					}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message","异常抛出！");
		}
		resMap.put("code", "500");
		resMap.put("message","删除失败！");
		return JSONObject.fromObject(resMap).toString();
	}



}
